package com.atm.atmsys.controller.TransactionController;

import com.atm.atmsys.Service.AccountService;
import com.atm.atmsys.Service.AtmService;
import com.atm.atmsys.Service.TransactionService;
import com.atm.atmsys.dto.*;
import com.atm.atmsys.entity.AtmInfo;
import com.atm.atmsys.entity.PageResult;
import com.atm.atmsys.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

import static org.apache.ibatis.ognl.OgnlOps.doubleValue;

@RestController
@RequestMapping("/transaction")
@Slf4j
public class TransactionController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    AtmService atmService;
    @Autowired
    AccountService accountService;
    @Autowired
    TransactionService transactionService;

    @PostMapping("/addTransaction")
    @ApiOperation("添加交易记录同时对数据库进行操作")
    public Result addTransaction(@RequestBody TransactionRecordDTO transactionRecordDTO) {
        log.info("添加交易记录");
//        Object atmIdObj = request.getSession().getAttribute("atmId");
//        if (atmIdObj == null || !(atmIdObj instanceof Integer)) {
//            return Result.error("请先登录ATM机");
//        }
//        int atmId = (Integer) atmIdObj;
//        System.out.println("返回的ATM的id为"+atmId);
        transactionRecordDTO.setAtmId(11111111);
        if(transactionRecordDTO.getStatus().equals("save")||transactionRecordDTO.getStatus().equals("withdraw")||transactionRecordDTO.getStatus().equals("transfer")){
            if (transactionRecordDTO.getStatus().equals("save")){
                if (accountService.getAccountByCardNO(transactionRecordDTO.getCardNo1())!=null) {
                    if(accountService.getAccountStatusByCardNO(transactionRecordDTO.getCardNo1()).equals("frozen")){
                        return Result.error("账户已被冻结");
                    }
                    SaveMoneyDTO saveMoneyDTO=new SaveMoneyDTO();
                    saveMoneyDTO.setCardNo(transactionRecordDTO.getCardNo1());
                    saveMoneyDTO.setMoney(doubleValue(transactionRecordDTO.getMoney()));
                    transactionRecordDTO.setCardNo2(saveMoneyDTO.getCardNo());


                        accountService.saveMoney(saveMoneyDTO);
                        transactionService.addTransaction(transactionRecordDTO);
                } else {

                    return Result.error("输入账户不存在");
                }
            }else if(transactionRecordDTO.getStatus().equals("withdraw")){
                if (accountService.getAccountByCardNO(transactionRecordDTO.getCardNo1())!=null) {
                    if(accountService.getAccountStatusByCardNO(transactionRecordDTO.getCardNo1()).equals("frozen")){
                        return Result.error("账户已被冻结");
                    }
                    DrawMoneyDTO drawMoneyDTO=new DrawMoneyDTO();
                    drawMoneyDTO.setCardNo(transactionRecordDTO.getCardNo1());
                    drawMoneyDTO.setMoney(doubleValue(transactionRecordDTO.getMoney()));
                    transactionRecordDTO.setCardNo2(drawMoneyDTO.getCardNo());

                        double balance=Double.valueOf(accountService.getBalanceByCardNO(drawMoneyDTO.getCardNo()));
                        if(balance>=drawMoneyDTO.getMoney()){
                            accountService.drawMoney(drawMoneyDTO);
                        }else {
                            return Result.error("余额不足");
                        }
                        transactionService.addTransaction(transactionRecordDTO);
                } else {
                    return Result.error("输入账户不存在");
                }
            }else if (transactionRecordDTO.getStatus().equals("transfer")){
                if (accountService.getAccountByCardNO(transactionRecordDTO.getCardNo1())!=null&&accountService.getAccountByCardNO(transactionRecordDTO.getCardNo2())!=null) {
                    if(accountService.getAccountStatusByCardNO(transactionRecordDTO.getCardNo1()).equals("frozen")||accountService.getAccountStatusByCardNO(transactionRecordDTO.getCardNo2()).equals("frozen")){
                        return Result.error("账户已被冻结");
                    }
                    DrawMoneyDTO drawMoneyDTO=new DrawMoneyDTO();
                    drawMoneyDTO.setCardNo(transactionRecordDTO.getCardNo1());
                    drawMoneyDTO.setMoney(doubleValue(transactionRecordDTO.getMoney()));

                        double balance=Double.valueOf(accountService.getBalanceByCardNO(drawMoneyDTO.getCardNo()));
                        if(balance>=drawMoneyDTO.getMoney()){
                            accountService.drawMoney(drawMoneyDTO);
                        }else {
                            return Result.error("余额不足");
                        }

                        SaveMoneyDTO saveMoneyDTO=new SaveMoneyDTO();
                        saveMoneyDTO.setCardNo(transactionRecordDTO.getCardNo2());
                        saveMoneyDTO.setMoney(doubleValue(transactionRecordDTO.getMoney()));
                        accountService.saveMoney(saveMoneyDTO);

                        transactionService.addTransaction(transactionRecordDTO);
                } else {
                    return Result.error("输入账户不存在");
                }

            }
        }else{
            return Result.error("请输入正确的交易状态");
        }
        return Result.success();
    }

    @PostMapping("/addTransactionWithoutChangeMysql")
    @ApiOperation("添加交易记录不对数据库进行操作")
    public Result addTransactionWithoutChangeMysql(@RequestBody TransactionRecordDTO transactionRecordDTO) {
        log.info("添加交易记录");
        Object atmIdObj = request.getSession().getAttribute("atmId");
        if (atmIdObj == null || !(atmIdObj instanceof Integer)) {
            return Result.error("无法获取有效的ATM ID");
        }
        int atmId = (Integer) atmIdObj;
        System.out.println("返回的ATM的id为"+atmId);
        transactionRecordDTO.setAtmId(atmId);
        if(transactionRecordDTO.getStatus().equals("save")||transactionRecordDTO.getStatus().equals("withdraw")||transactionRecordDTO.getStatus().equals("transfer")){
            if (transactionRecordDTO.getStatus().equals("save")){

                transactionService.addTransactionWithoutChangeMysql(transactionRecordDTO);
            }else if(transactionRecordDTO.getStatus().equals("withdraw")){

                transactionService.addTransactionWithoutChangeMysql(transactionRecordDTO);
            }else if (transactionRecordDTO.getStatus().equals("transfer")){

                transactionService.addTransactionWithoutChangeMysql(transactionRecordDTO);

            }
        }else{
            return Result.error("请输入正确的交易状态");
        }
        return Result.success();
    }

    //删除指定ID的交易记录
    @DeleteMapping("/deleteTransaction/{id}")
    @ApiOperation("删除指定ID的交易记录")
    public Result deleteTransaction(@PathVariable int id) {
        log.info("删除交易记录");
        transactionService.deleteTransaction(id);
        return Result.success();
    }

    //查询所有交易记录
    @GetMapping("/page")
    @ApiOperation("分页查询交易记录")
    public Result<PageResult> page(TransactionPageQueryDTO transactionPageQueryDTO){
            log.info("交易记录分页查询，参数为：{}",transactionPageQueryDTO);
            PageResult pageResult=transactionService.pageQuery(transactionPageQueryDTO);
            return Result.success(pageResult);


    }


    //根据卡号查询所有交易记录
    @GetMapping("/pageByCardNo")
    @ApiOperation("分页查询指定卡号的交易记录")
    public Result<PageResult> pageByCardNo(TransactionPageByCardNo transactionPageByCardNo){
        log.info("交易记录分页查询，参数为：{}",transactionPageByCardNo);
        PageResult pageResult=transactionService.pageByCardNo(transactionPageByCardNo);
        return Result.success(pageResult);

    }

}
